package com.hh.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.commonutils.Result;
import com.hh.eduservice.entity.EduTeacher;
import com.hh.eduservice.entity.vo.TeacherQuery;
import com.hh.eduservice.mapper.EduTeacherMapper;
import com.hh.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author huhuan
 * @since 2021-09-04
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @ApiOperation(value = "根据ID查询讲师信息")
    @GetMapping("getTeacherById/{id}")
    public Result getTeacherById(@PathVariable long id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return Result.success().data("data", teacher);
    }

    @ApiOperation(value = "所有讲师列表" )
    @GetMapping("findAll")
    public Result selectAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("items", list);
        return Result.success().data("items", list);
    }

    @ApiOperation(value = "删除id为x的讲师信息")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable String id){
        boolean b = eduTeacherService.removeByIds(Arrays.asList(id));
        return Result.success().setMsg(b == true ? "删除成功" : "删除失败");
    }

    @ApiOperation(value = "增加一个讲师信息")
    @PostMapping("add")
    public Result add(@RequestBody(required = true) EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        return Result.success().setMsg(save == true ? "增加成功" : "增加失败");
    }

    @ApiOperation(value = "实现条件分页查询")
    @PostMapping("/pageQuery/{current}/{limit}")
    public Result pageQueryCond(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper();
        String name = teacherQuery.getName() == null ? "" : teacherQuery.getName();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        String level = teacherQuery.getLevel() == null ? "" : teacherQuery.getLevel();
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level", level);
        }
        String begin = (teacherQuery.getBegin() == null ? "" : teacherQuery.getBegin());
        String end = teacherQuery.getEnd() == null ? "" : teacherQuery.getEnd();
        if (!StringUtils.isEmpty(begin) && !StringUtils.isEmpty(end)) {
            queryWrapper.ge("gmt_create", begin);
            queryWrapper.le("gmt_modified", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page, queryWrapper);
        long total = page.getTotal();
        long current1 = page.getCurrent();
        long size = page.getSize();
        List<EduTeacher> records = page.getRecords();
        Map<String, Object> objectObjectMap = new HashMap<>();
        objectObjectMap.put("total", total);
        objectObjectMap.put("current", current1);
        objectObjectMap.put("size", size);
        objectObjectMap.put("items", records);
        return Result.success().data(objectObjectMap);
    }

    @ApiOperation(value = "更新教师信息")
    @PostMapping("/updateEduTeacher")
    public Result updateEduTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
        if (StringUtils.isEmpty(eduTeacher.getId())) {
            return Result.fail().setMsg("Id不能为空");
        } else {
            eduTeacherService.updateById(eduTeacher);
            return Result.success();
        }
    }

    @GetMapping("/getTeacherByMapperId/{ids}")
    public Result getTeacherByMapperId(@PathVariable String ids){
        HashMap<String, Object> map = new HashMap<>();
        String[] ids$ = ids.split(",");
        map.put("ids", ids$);
        ArrayList<EduTeacher> eduTeacher = (ArrayList<EduTeacher>) eduTeacherMapper.getTeacherByMapperId(map);
        return Result.success().data("teacher", eduTeacher);
    }
}

